package com.strawcom.charmring;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.strawcom.charmring.util.BaseUtil;
import com.strawcom.charmring.util.PreferenceUtil;
import com.strawcom.charmring.util.ViewUtil;

/**
 * 
 * @ClassName BaseActivity
 * @Description 所有活动应该继承到基类<br>
 *              提供了活动常用的对象和方法
 * @author liyongqiang
 * @date 2015-3-30 上午12:02:52
 * 
 */
public class BaseActivity extends FragmentActivity {

	protected Context context;

	@Override protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		ActivityCollector.addActivity(this);

		ViewUtil.changeFontsStyle(this);
	}

	@Override protected void onDestroy() {
		super.onDestroy();
		ActivityCollector.removeActivity(this);
	}

	@Override public void startActivity(Intent intent) {
		super.startActivity(intent);
		overridePendingTransition(R.anim.slide_right_enter, R.anim.slide_left_exit);
	}

	@Override public void finish() {
		super.finish();
		if (ActivityCollector.size() > 1) {
			overridePendingTransition(R.anim.slide_left_enter, R.anim.slide_right_exit);
		}
	}

	public void actionStart(Class<?> target, Object... params) {
		actionStart(target, BaseUtil.initBundle(params));
	}

	public void actionStart(Class<?> target, Bundle bundle) {
		Intent intent = new Intent(this, target);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}

	public void showToast(int resId) {
		showToast(getString(resId));
	}

	public void showToast(String text) {
		Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(!PreferenceUtil.readBoolean(this, C.sp.isBindCidSueecss)){
			String cid=PreferenceUtil.readString(this, C.sp.cid,"");
			if(!"".equals(cid)){
				App.i().uploadCid(cid);
			}
		}
	}
	
	/** http开头 */
	public void openBrowser(String uri){
	   Intent  intent = new  Intent(Intent.ACTION_VIEW, Uri.parse(uri));
	   startActivity(intent);
	}
	
	public void closeInputMethod() {
		((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);   
	}
}
